@echo off
setlocal enabledelayedexpansion

echo ==========================================
echo Sokit CLI Windows 构建脚本
echo ==========================================

REM 检查必要的工具
echo 检查依赖...

where cmake >nul 2>&1
if %errorlevel% neq 0 (
    echo 错误: 未找到 cmake
    echo 请安装 CMake: https://cmake.org/download/
    exit /b 1
)

where g++ >nul 2>&1
if %errorlevel% neq 0 (
    echo 错误: 未找到 g++
    echo 请安装 MinGW 或 Visual Studio
    exit /b 1
)

echo 依赖检查完成

REM 清理构建目录
echo 清理构建目录...
if exist build-cli rmdir /s /q build-cli
mkdir build-cli

REM 配置构建
echo 配置构建...
cd build-cli

cmake .. ^
    -DCMAKE_BUILD_TYPE=Release ^
    -DCMAKE_AUTOMOC=ON ^
    -DCMAKE_AUTOUIC=ON ^
    -DCMAKE_AUTORCC=ON ^
    -G "MinGW Makefiles"

if %errorlevel% neq 0 (
    echo 配置失败，尝试使用默认生成器...
    cmake .. ^
        -DCMAKE_BUILD_TYPE=Release ^
        -DCMAKE_AUTOMOC=ON ^
        -DCMAKE_AUTOUIC=ON ^
        -DCMAKE_AUTORCC=ON
)

if %errorlevel% neq 0 (
    echo 错误: CMake 配置失败
    cd ..
    exit /b 1
)

cd ..

REM 构建项目
echo 构建项目...
cd build-cli

cmake --build . --target sokit-cli --config Release

if %errorlevel% neq 0 (
    echo 错误: 构建失败
    cd ..
    exit /b 1
)

cd ..

REM 创建发布包
echo 创建发布包...

set PACKAGE_DIR=sokit-cli-windows
if exist %PACKAGE_DIR% rmdir /s /q %PACKAGE_DIR%
mkdir %PACKAGE_DIR%

REM 复制可执行文件
copy build-cli\bin\sokit-cli.exe %PACKAGE_DIR%\

REM 创建README
echo Sokit CLI - Windows 命令行版本 > %PACKAGE_DIR%\README.txt
echo ================================ >> %PACKAGE_DIR%\README.txt
echo. >> %PACKAGE_DIR%\README.txt
echo 这是一个高性能的TCP/UDP数据转发工具的命令行版本。 >> %PACKAGE_DIR%\README.txt
echo. >> %PACKAGE_DIR%\README.txt
echo 使用方法: >> %PACKAGE_DIR%\README.txt
echo --------- >> %PACKAGE_DIR%\README.txt
echo sokit-cli [选项] ^<源地址:端口^> ^<目标地址:端口^> >> %PACKAGE_DIR%\README.txt
echo. >> %PACKAGE_DIR%\README.txt
echo 选项: >> %PACKAGE_DIR%\README.txt
echo   -t, --tcp        使用TCP协议 >> %PACKAGE_DIR%\README.txt
echo   -u, --udp        使用UDP协议 >> %PACKAGE_DIR%\README.txt
echo   -v, --verbose    详细输出 >> %PACKAGE_DIR%\README.txt
echo   -q, --quiet      静默模式 >> %PACKAGE_DIR%\README.txt
echo   -s, --stats      显示传输统计 >> %PACKAGE_DIR%\README.txt
echo   --timeout ^<秒^>   连接超时时间 (默认: 30秒) >> %PACKAGE_DIR%\README.txt
echo   -h, --help       显示帮助信息 >> %PACKAGE_DIR%\README.txt
echo   --version        显示版本信息 >> %PACKAGE_DIR%\README.txt
echo. >> %PACKAGE_DIR%\README.txt
echo 示例: >> %PACKAGE_DIR%\README.txt
echo ------ >> %PACKAGE_DIR%\README.txt
echo # TCP转发: 将本地8080端口转发到远程服务器9090端口 >> %PACKAGE_DIR%\README.txt
echo sokit-cli -t 127.0.0.1:8080 192.168.1.100:9090 >> %PACKAGE_DIR%\README.txt
echo. >> %PACKAGE_DIR%\README.txt
echo # UDP转发: 将本地9090端口转发到远程服务器8080端口 >> %PACKAGE_DIR%\README.txt
echo sokit-cli -u 127.0.0.1:9090 192.168.1.100:8080 >> %PACKAGE_DIR%\README.txt
echo. >> %PACKAGE_DIR%\README.txt
echo # 显示详细信息和统计 >> %PACKAGE_DIR%\README.txt
echo sokit-cli -t -v -s 127.0.0.1:8080 192.168.1.100:9090 >> %PACKAGE_DIR%\README.txt
echo. >> %PACKAGE_DIR%\README.txt
echo # 静默模式运行 >> %PACKAGE_DIR%\README.txt
echo sokit-cli -t -q 127.0.0.1:8080 192.168.1.100:9090 >> %PACKAGE_DIR%\README.txt
echo. >> %PACKAGE_DIR%\README.txt
echo 特性: >> %PACKAGE_DIR%\README.txt
echo ----- >> %PACKAGE_DIR%\README.txt
echo - 高性能TCP/UDP数据转发 >> %PACKAGE_DIR%\README.txt
echo - 支持高并发连接 >> %PACKAGE_DIR%\README.txt
echo - 实时传输统计 >> %PACKAGE_DIR%\README.txt
echo - 详细的连接日志 >> %PACKAGE_DIR%\README.txt
echo - 内存优化的缓冲区管理 >> %PACKAGE_DIR%\README.txt
echo - 跨平台支持 >> %PACKAGE_DIR%\README.txt
echo. >> %PACKAGE_DIR%\README.txt
echo 系统要求: >> %PACKAGE_DIR%\README.txt
echo --------- >> %PACKAGE_DIR%\README.txt
echo - Windows 10/11 >> %PACKAGE_DIR%\README.txt
echo - Qt6 Runtime Libraries >> %PACKAGE_DIR%\README.txt
echo - 网络连接 >> %PACKAGE_DIR%\README.txt
echo. >> %PACKAGE_DIR%\README.txt
echo 安装: >> %PACKAGE_DIR%\README.txt
echo ------ >> %PACKAGE_DIR%\README.txt
echo 1. 解压此包到任意目录 >> %PACKAGE_DIR%\README.txt
echo 2. 确保系统已安装Qt6运行时库 >> %PACKAGE_DIR%\README.txt
echo 3. 运行: sokit-cli.exe --help >> %PACKAGE_DIR%\README.txt
echo. >> %PACKAGE_DIR%\README.txt
echo 许可证: >> %PACKAGE_DIR%\README.txt
echo ------- >> %PACKAGE_DIR%\README.txt
echo GPL v3 - 详见LICENSE文件 >> %PACKAGE_DIR%\README.txt

REM 创建安装脚本
echo @echo off > %PACKAGE_DIR%\install.bat
echo echo 安装 Sokit CLI... >> %PACKAGE_DIR%\install.bat
echo. >> %PACKAGE_DIR%\install.bat
echo set INSTALL_DIR=%%PROGRAMFILES%%\SokitCLI >> %PACKAGE_DIR%\install.bat
echo if not exist "%%INSTALL_DIR%%" mkdir "%%INSTALL_DIR%%" >> %PACKAGE_DIR%\install.bat
echo. >> %PACKAGE_DIR%\install.bat
echo copy sokit-cli.exe "%%INSTALL_DIR%%\" >> %PACKAGE_DIR%\install.bat
echo. >> %PACKAGE_DIR%\install.bat
echo echo 安装完成! >> %PACKAGE_DIR%\install.bat
echo echo 使用方法: "%%INSTALL_DIR%%\sokit-cli.exe" --help >> %PACKAGE_DIR%\install.bat
echo. >> %PACKAGE_DIR%\install.bat
echo echo 注意: 请将 %%INSTALL_DIR%% 添加到系统PATH环境变量中 >> %PACKAGE_DIR%\install.bat

REM 创建卸载脚本
echo @echo off > %PACKAGE_DIR%\uninstall.bat
echo echo 卸载 Sokit CLI... >> %PACKAGE_DIR%\uninstall.bat
echo. >> %PACKAGE_DIR%\uninstall.bat
echo set INSTALL_DIR=%%PROGRAMFILES%%\SokitCLI >> %PACKAGE_DIR%\uninstall.bat
echo. >> %PACKAGE_DIR%\uninstall.bat
echo if exist "%%INSTALL_DIR%%\sokit-cli.exe" ( >> %PACKAGE_DIR%\uninstall.bat
echo     del "%%INSTALL_DIR%%\sokit-cli.exe" >> %PACKAGE_DIR%\uninstall.bat
echo     echo 已删除: %%INSTALL_DIR%%\sokit-cli.exe >> %PACKAGE_DIR%\uninstall.bat
echo ) else ( >> %PACKAGE_DIR%\uninstall.bat
echo     echo 未找到安装的文件: %%INSTALL_DIR%%\sokit-cli.exe >> %PACKAGE_DIR%\uninstall.bat
echo ) >> %PACKAGE_DIR%\uninstall.bat
echo. >> %PACKAGE_DIR%\uninstall.bat
echo echo 卸载完成! >> %PACKAGE_DIR%\uninstall.bat

REM 创建压缩包
powershell -Command "Compress-Archive -Path '%PACKAGE_DIR%' -DestinationPath 'sokit-cli-windows.zip' -Force"

echo.
echo ==========================================
echo 构建完成!
echo ==========================================
echo 可执行文件: build-cli\bin\sokit-cli.exe
echo 发布包: sokit-cli-windows.zip
echo.
echo 测试命令:
echo   build-cli\bin\sokit-cli.exe --help
echo.

pause 